<?php defined('InNcPHP') or exit('Access Invalid!');?>
<script src="//webapi.amap.com/maps?v=1.3&key=9278aff6ea76c052364dd8216fc9be47&plugin=AMap.Geocoder"></script>
<style>
    .show { display: block; }
    .hide { display: none; }
</style>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <h3>美食</h3>
            <?php echo $output['top_link'];?>
        </div>
    </div>
    <div class="fixed-empty"></div>
    <!-- 提交表单开始 -->
    <form id="ticket_form" method="post" enctype="multipart/form-data">
        <input type="hidden" name="form_submit" value="ok" />
        <table class="table tb-type2 nobdb">
            <tbody>
            <!-- 所属商家 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_index_merchant'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select name="merchant_id">
                        <option value=""><?php echo $lang['nc_please_choose'];?>...</option>
                        <?php if (is_array($output['merchants'])): ?>
                            <?php foreach ($output['merchants'] as $k => $v): ?>
                                <option value="<?php echo $v['merchant_id']; ?>"><?php echo $v['mc_shop_name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 名称 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">名称:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="title" class="txt"></td>
                <td class="vatop tips"><?php echo $lang['ticket_add_title_notice'];?></td>
            </tr>
            <!-- 副标题 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">副标题:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="fd_subtitle" class="txt"></td>
                <td class="vatop tips">仅在代金券类型下才会显示该标题,例：仅售88元！价值100元的代金券1份仅适用于适用范围：菜金，可叠加使用，提供免费WiFi。</td>
            </tr>
            <!-- 分类 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_index_class'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select id="cate1" name="cate1" class="class-select">
                        <option value="">选择分类</option>
                        <?php if (is_array($output['cate'])): ?>
                            <?php foreach ($output['cate'] as $k => $v): ?>
                                <option value="<?php echo $v['tc_id']; ?>" <?php echo $output['hotel_info']['cate_id'] == $v['tc_id'] ? 'selected' : ''; ?>><?php echo $v['tc_name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>

            <!-- 类型，固定价格或者每日售价或者不可购买 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation">类型:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                   <label><input id="type_t" type="radio" name="type" value="0" checked> 优惠团购 </label>
                   <label><input id="type_d" type="radio" name="type" value="1"> 代金券</label>
                    <input value="0" name="food_type" type="hidden">
                </td>
                <td class="vatop tips"><?php echo $lang['ticket_add_type_notice'];?></td>
            </tr>

            <tr class="add_open_spec">
                <td colspan="2" class="required"><label class="validation">是否可购买:</label> </td>
            </tr>
            <tr class="noborder add_open_spec">
                <td class="vatop rowform onoff">
                    <label for="is_purchase1" class="cb-enable selected "><span><?php echo $lang['nc_yes'];?></span></label>
                    <label for="is_purchase0" class="cb-disable "><span><?php echo $lang['nc_no'];?></span></label>
                    <input id="is_purchase1" name="is_purchase"  checked value="1" type="radio">
                    <input id="is_purchase0" name="is_purchase" value="0" type="radio"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 地址 -->
            <tr>
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_area'];?>: </label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select id="store_areaId_01" name="province_id" class="class-select">
                        <option value="">省份</option>
                        <?php if (is_array($output['provinces'])): ?>
                            <?php foreach ($output['provinces'] as $k => $v): ?>
                                <option value="<?php echo $v['area_id']; ?>"><?php echo $v['name']; ?></option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                    <select id="store_areaId_02" name="city_id" class="class-select">
                        <option value="">城市</option>
                    </select>
                    <select id="store_areaId_03" name="area_id" class="class-select">
                        <option value="">区/县</option>
                    </select>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 详细地址 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_addr'];?>:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" name="addr" id="addr" class="txt"></td>
                <td class="vatop tips"><?php echo $lang['ticket_add_addr_notice'];?></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation" for="store_location">店铺经纬度lng&lat:</label></td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" value="" name="store_location" readonly="true" id="store_location" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>

            <tr class="noborder">
                <td class="vatop rowform"><button type="button" class="btn" id="getMapLocation">地图获取坐标</button></td>
                <td class="vatop tips"></td>
            </tr>
            <tr class="add_open_spec">
                <td colspan="2" class="required"><label class="validation">是否开启规格:</label> </td>
            </tr>
            <tr class="noborder add_open_spec">
                <td class="vatop rowform onoff">
                    <label for="open_spec1" class="cb-enable "><span><?php echo $lang['nc_yes'];?></span></label>
                    <label for="open_spec0" class="cb-disable selected"><span><?php echo $lang['nc_no'];?></span></label>
                    <input id="open_spec1" name="open_spec"  value="1" type="radio">
                    <input id="open_spec0" name="open_spec" checked  value="0" type="radio"></td>
                <td class="vatop tips"></td>
            </tr>


            <tr class="open_spec" style="display: none">
                <th colspan="2">
                    <a href="JavaScript:void(0);"  class="add_goods_size"  style="display:block; width: 65px; height: 25px; line-height: 1.5rem; background-color: #24CA71;color: #FFF; text-align: center">+ 新增规格</a>
                    您可以添加多种种规格；规格名称可以自定义，且每种规则的价格和库存都的设置；如：500ml
                </th>
            </tr>


            <tr class="open_spec" style="display: none">
                <td colspan="2">
                    <table class="table tb-type2 goods ">
                        <tbody class="goods_size1">
                        <tr>
                            <th>序号</th>
                            <th>规格</th>
                            <th>标签</th>
                            <th>原价</th>
                            <th>现价</th>
                            <th>库存</th>
                            <th>是否上架</th>
                            <th> 操作</th>
                        </tr>

                        <tr >
                            <td class="w10">1<input type="hidden" name="spec_sort[]"  class="spec_sort" value="1"></td>
                            <td class="w15"><input type="text" name="spec_name[]"  class="spec_name"></td>
                            <td class="w15"><input type="text" value="" name="spec_label[]"  class="spec_label"></td>
                            <td class="w15"><input type="text" value="0" name="spec_market_price[]"  class="market_price"></td>
                            <td class="w15"><input type="text" value="0" name="spec_price[]"  class="spec_price"></td>
                            <td class="w10"><input type="text" value="0" name="spec_stock[]"  class="spec_stock"></td>
                            <td class="w10">
                                <select name="spec_is_show[]">
                                    <option value="1" selected >上架</option>
                                    <option value="0" >下架</option>
                                </select>
                            </td>
                            <td class="w10"></td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>

            <style type="text/css">
                .delete_size{
                    display:block;
                    width: 50px;
                    height: 25px;
                    line-height: 1.5rem;
                    background-color: #ff2e47;
                    color: #FFF;
                    text-align: center
                }
            </style>

            <script>
                var i = 1;
                $('.add_goods_size').click(function () {
                    $('.delete').hide();
                    i++;
                    var html = ' <tr ><td class="w10">'+i+'<input type="hidden" name="spec_sort[]"  class="spec_sort" value="'+i+'"> </td> <td class="w15"><input type="text" ' +
                        ' name="spec_name[]"  class="spec_name"></td>  <td class="w15"><input type="text" value=""  ' +
                        ' name="spec_label[]"  class="spec_label"></td> '+
                        ' <td class="w15"><input type="text" value="0"  ' +
                        ' name="spec_market_price[]"  class="spec_market_price"></td>  ' +
                        '<td class="w15"><input type="text" value="0" name="spec_price[]"  class="spec_price">' +
                        '</td><td class="w10"><input type="text" value="0" name="spec_stock[]"  class="spec_stock">' +
                        ' <td class="w10"><select name="spec_is_show[]"><option value="1" selected >上架</option> <option value="0" >下架</option></select></td>'+
                        '</td> ' +
                        ' <td class="w10"><a class="delete_size delete" href="JavaScript:void(0);" >删除</a></td></tr>';
                    $('.goods_size1').append(html);
                });

                $('.goods_size1').on('click','.delete',function () {
                    i--;
                    //上一个 打开删除
                    $('.goods_size1').find('tr').eq($(this).parent().parent().index()-1).find('.delete').show();
                    $(this).parent().parent('tr').remove();
                });

            </script>



            <!-- 标签 -->
            <tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="validation">标签:</label>
                </td>
            </tr>
            <tr class="noborder close_spec">
                <td class="vatop rowform"><input type="text" name="label" class="txt"  ></td>
                <td class="vatop tips"></td>
            <!-- 默认显示价格 -->
            <tr class="noborder deduction" style="display:none;">
                <td colspan="2" class="required">
                    <label class="validation">抵扣金额:</label>
                </td>
            </tr>
            <tr class="noborder deduction" style="display:none;">
                <td class="vatop rowform"><input type="text" name="deduction" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 默认显示价格 -->
            <tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="validation">原价:</label>
                </td>
            </tr>
            <tr class="noborder close_spec">
                <td class="vatop rowform"><input type="text" name="show_price" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 默认显示价格 -->
            <tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="validation">现价:</label>
                </td>
            </tr>
            <tr class="noborder close_spec">
                <td class="vatop rowform"><input type="text" name="price" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>
            <!-- 成本价 -->
            <!--<tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="validation">成本价:</label>
                </td>
            </tr>
            <tr class="noborder close_spec">
                <td class="vatop rowform"><input type="text" name="cost_price_" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>-->
            <!-- 默认显示价格 -->
            <tr class="noborder close_spec">
                <td colspan="2" class="required">
                    <label class="">库存:</label>
                </td>
            </tr>
            <tr class="noborder close_spec">
                <td class="vatop rowform"><input type="text" name="stock" class="txt"></td>
                <td class="vatop tips"></td>
            </tr>

            <!-- 封面图片 -->
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation"><?php echo $lang['ticket_add_image'];?>:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><span class="type-file-show">
                    </span> <span class="type-file-box">
                        <input type='text' name='textfield' id='textfield' class='type-file-text' />
                        <input type='button' name='button' id='button' value='' class='type-file-button' />
                        <input name="thumb" type="file" class="type-file-file" id="image" size="30" hidefocus="true" />
                    </span>
                </td>
                <td class="vatop tips"><?php echo $lang['ticket_add_image_notice'];?></td>
            </tr>

            <!-- 菜品介绍 -->
            <tr class="noborder deduction1">
                <td colspan="2" class="required">
                    <label class="validation">菜品介绍:</label>
                </td>
            </tr>
            <tr class="noborder deduction1" >
                <td colspan="2" class="vatop rowform">
                    <textarea id="content" name="content" style="width:70%;height: 400px;"></textarea>
                </td>
            </tr>
            <!-- 购买需知 -->
            <tr class="noborder " >
                <td colspan="2" class="required">
                    <label class="validation">购买需知:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td colspan="2" class="vatop rowform">
                    <textarea id="content1" name="content1" style="width:70%;height: 400px;"></textarea>
                </td>
            </tr>
            </tbody>


            <!-- 提交 -->
            <tr class="tfoot">
                <td colspan="2" >
                    <a href="JavaScript:void(0);" class="btn" id="submitBtn">
                        <span><?php echo $lang['nc_submit'];?></span>
                    </a>
                </td>
            </tr>
            </tfoot>
        </table>
    </form>
</div>

<script type="text/javascript" SRC="./resource/kindeditor/kindeditor-min.js"></script>
<script>
    $(function(){
        //类型改变事件（团购优惠、抵扣券）
        $('input[name="type"]').change(function () {
            $("[name='food_type']").val(this.value);
            if (this.value == 0) {
                $('.deduction').hide();
                $('.deduction1').show();
                $(".add_open_spec").show(300);
            } else {
                $('.deduction').show();
                $('.deduction1').hide();
                $(".add_open_spec").hide(300);

                $('.open_spec').hide();
                $('.close_spec').show();
            }
        });


        $('input[name="open_spec"]').change(function () {
            $("[name='open_spec']:checked").val(this.value);
            if (this.value == 0) {
                $('.open_spec').hide();
                $('.close_spec').show();
            } else {
                $('.open_spec').show();
                $('.close_spec').hide();
            }
        });



        //实例化富文本编辑器
        KindEditor.ready(function(K){
            K.create('textarea[name="content"]', {
                themeType: 'simple',
                resizeType: 1,
                uploadJson: './resource/kindeditor/php/upload_json.php',
                fileManagerJson: './resource/kindeditor/php/file_manager_json.php',
                allowFileManager: true,
                afterBlur: function(){this.sync();}
            });
            K.create('textarea[name="content1"]', {
                themeType: 'simple',
                resizeType: 1,
                uploadJson: './resource/kindeditor/php/upload_json.php',
                fileManagerJson: './resource/kindeditor/php/file_manager_json.php',
                allowFileManager: true,
                afterBlur: function(){this.sync();}
            });
        });
        //选择省份
        $("#store_areaId_01").change(function(){

            var _areaID = $(this).val();
            _this = $("#store_areaId_01");
            if(_areaID){
                $.getJSON("./index.php?act=common&op=getAreaChild&parent_id=" + _areaID, function(data){
                    $obj = $("#store_areaId_02");
                    $obj.empty();
                    $obj.append('<option value="">城市</option>');
                    $obj.nextAll("select").empty();
                    $obj.nextAll("select").append('<option value="">区/县</option>');
                    $.each(data, function(i,item){
                        $obj.append('<option value="'+item.area_id+'">'+item.name+'</option>');
                    });
                });
            }else{
                _this.nextAll("select").empty();
                _this.next("select").append('<option value="">城市</option>');
                _this.next("select").next('select').append('<option value="">区/县</option>');
            }
        });
        //选择城市
        $("#store_areaId_02").change(function(){
            var _areaID = $(this).val();
            _this = $("#store_areaId_2");
            if(_areaID){
                $.getJSON("index.php?act=common&op=getAreaChild&parent_id=" + _areaID, function(data){
                    $obj = $("#store_areaId_03");
                    $obj.empty();
                    $obj.append('<option value="">区/县</option>');
                    $.each(data, function(i,item){
                        $obj.append('<option value="'+item.area_id+'">'+item.name+'</option>');
                    });
                });
            }else{
                _this.nextAll("select").empty();
                _this.next("select").append('<option value="">区/县</option>');
            }
        });
        //选择区/县
        $("#store_areaId_03").change(function(){
            var _areaID = $(this).val();
            if(_areaID){
                var area_p = $("#store_areaId_01").find("option:selected").text();
                var area_c = $("#store_areaId_02").find("option:selected").text();
                var area_a = $("#store_areaId_03").find("option:selected").text();
            }
        });

        //图片文件选择
        $("#image").change(function(){
            $("#textfield").val($(this).val());
        });
        //图片文件选择
        $("#image1").change(function(){
            $("#textfield1").val($(this).val());
        });

        //提交表单
        $("#submitBtn").click(function(){
            if ($("#ticket_form").valid()) {
                $("#ticket_form").submit();
            } else {
                $('html,body').animate({scrollTop: $('label.error:visible:eq(0)').offset().top - 70}, 300);
            }
        });

        //获取经纬度
        $(document).on("click","#getMapLocation",function(){
            var proviceId= $("#store_areaId_01").val();
            var cityId = $("#store_areaId_02").val();
            var areaId = $("#store_areaId_03").val();
            if(cityId == ''){
                alert('请先选择所在地区');
                return false;
            }
            /*    if($("#store_areaId_3 option").length!=1&&areaId == ''){
             alert('请先选择所在区县');
             return false;
             }*/
            $.ajax({
                type: "GET",
                url: "index.php",
                data: {act:'common',op:'bdmap',proviceId:proviceId,cityId:cityId,areaId:areaId,store_address:$("#addr").val()},
                dataType: "html",
                success: function(data){
                    $('body').append(data);
                }
            });
        });
        //表单校验
        $("#ticket_form").validate({
            errorPlacement: function(error, element){
                if (element.attr('type') === 'file') {
                    error.appendTo(element.parent().parent().parent().prev().find('td:first'));
                } else {
                    error.appendTo(element.parent().parent().prev().find('td:first'));
                }
            },
            rules: {
                merchant_id: 'required',
                title: {
                    required: true,
                    minlength: 3,
                    maxlength: 20
                },
                subtitle: {
                    required: true,
                    minlength: 3,
                    maxlength: 255
                },
                cate1: 'required',
                /*cate2: 'required',*/

                /*show_price: {
                    required: true,
                    number: true,
                    min: 0.01,
                    max: 9999999
                },*/
                /*market_price: {
                    required: true,
                    min: 0.01,
                    max: 9999999
                },
                price:{
                    required: true,
                    number: true,
                    min: 0.01,
                    max: 9999999
                },*/
                province_id: 'required',
                city_id: 'required',
                area_id: 'required',
                addr: 'required',
                thumb: 'required',
                face: 'required',
                content: {
                    required:check_type,
                },
                content1: 'required',
                store_location:'required',
                hotel_tel:'required',
                deduction:{
                    required:checktype,
                },
                cost_price_:'required',
//                stock:'required',
//                label:'required',


            },
            messages : {
                merchant_id: '<?php echo $lang['ticket_add_merchant_null']; ?>',
                title: '<?php echo $lang['ticket_add_title_notice']; ?>',
                subtitle: '<?php echo $lang['ticket_add_title_notice']; ?>',
                cate1: '请选择分类',
                cate2: '<?php echo $lang['ticket_add_cate2_null']; ?>',
//                price:'售价不能为空',
//                show_price: '<?php //echo $lang['ticket_add_price_notice']; ?>//',
//                market_price: '<?php //echo $lang['ticket_add_price_notice']; ?>//',
                province_id: '<?php echo $lang['ticket_add_province_null']; ?>',
                city_id: '<?php echo $lang['ticket_add_city_null']; ?>',
                area_id: '<?php echo $lang['ticket_add_area_null']; ?>',
                addr: '<?php echo $lang['ticket_add_addr_null']; ?>',
                thumb: '<?php echo $lang['ticket_add_image_null']; ?>',
                face: '<?php echo $lang['ticket_add_image_null']; ?>',
                content: {
                    required:'菜品介绍不能为空',
                },
                content1: '<?php echo $lang['ticket_add_tips_null']; ?>',
                store_location:'地理坐标不能为空',
                hotel_tel:'酒店电话不能为空',
                deduction:{
                    required:'抵扣金额不能为空',
                },
                cost_price_:'协议价不能为空',
//                stock:'库存不能为空',
//                label:'标签不能为空'
            }
        });
        function checktype()
        {
            if($("[name='food_type']").val()==1){
                return true;
            }else{
                return false;
            }

        }
        function check_type()
        {
            if($("[name='food_type']").val()==1){
                return false;
            }else{
                return true;
            }
        }
    })

</script>
